<?php
    class Admincp_ProviderController extends Main_Controller_Action
    {
        private $_arrParam;
        private $_model;
        private $_validate;
        
        public function init()
        {
            $this->loadTemplates('admin');
            $this->_arrParam = $this->_request->getParams();
            $this->_model = new Admincp_Model_Provider();
            $this->_validate = new Admincp_Form_ValidateProvider();
            $this->view->headTitle('Quản lý nhà cung cấp');
        }
        
        public function preDispatch()
        {
            $auth = Zend_Auth::getInstance();
            if(!$auth->hasIdentity()) {
                if($this->_request->getActionName() != 'login')
                    $this->_redirect('admincp/index/login');
            }
        }
        
        public function indexAction()
        {
            $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->listItem());
			$paginator = new Zend_Paginator($adapter);
			$paginator->setItemCountPerPage(40);
			$paginator->setPageRange(5);
			$currentPage = $this->_request->getParam('page', 1);
			$paginator->setCurrentPageNumber($currentPage);
			$this->view->data = $paginator;
			$this->view->total = $paginator->getTotalItemCount();

            if($_POST['add']) {
                $this->_redirect('admincp/provider/create');
            }
            if($_POST['delete']) {
                $listId = $this->_arrParam['check'];
			    $this->_model->deleteMultiItem($listId);
			    $this->_redirect($_SERVER['HTTP_REFERER']);
            }
        }
        
        public function createAction()
        {
            if($this->_request->isPost()) {
                $name = trim(strip_tags($this->_arrParam['name']));
                if(!$this->_validate->validName($name)) {
                    $this->view->errorName = 'Tên nhà cung cấp từ 1-100 kí tự';
                }
                else {
                    $strFlex = new Zendvn_Filter_RemoveCircumflex();
                    $data = array(
                        'name' => $name,
                        'url' => $strFlex->filter2(strtolower($name)),
                        'status' => 1,
                    );
                    $this->_model->saveItem($data, 'create');
                    $this->_redirect('admincp/provider');
                }
            }
        }

        public function editAction()
        {
            $id = (int) $this->_arrParam['id'];
            $this->view->data = $this->_model->getItem($id);
            
            if($this->_request->isPost()) {
                $name = trim(strip_tags($this->_arrParam['name']));
                if(!$this->_validate->validName($name)) {
                    $this->view->errorName = 'Tên nhà cung cấp từ 1-100 kí tự';
                }
                else {
                    $strFlex = new Zendvn_Filter_RemoveCircumflex();
                    $data = array(
                        'id' => $id,
                        'name' => $name,
                        'url' => $strFlex->filter2(strtolower($name)),
                    );
                    $this->_model->saveItem($data, 'update');
                    $this->_redirect('admincp/provider');
                }
            }
        }
        
        public function deleteAction()
        {
            $id = (int) $this->_arrParam['id'];
		    $this->_model->deleteItem($id);
		    $this->_redirect($_SERVER['HTTP_REFERER']);
        }
        
        public function publishAction()
		{
		    $id = (int) $this->_arrParam['id'];
		    $status = (int) $this->_arrParam['status'];
		    $status = ($status == 1) ? 2 : 1;
		    $data = array(
                'id' => $id,
		        'status' => $status,
		    );
		    $this->_model->saveItem($data, 'update');
		    $this->_redirect($_SERVER['HTTP_REFERER']);
		}
        
        public function searchAction()
		{
		    $strFlex = new Zendvn_Filter_RemoveCircumflex();
		    $name = strtolower(trim($strFlex->filter(strip_tags($this->_arrParam['name']))));
		    $status = (int) $this->_arrParam['status'];
		    $this->view->keyword = array(
	            'name' => $this->_arrParam['name'],
	            'status' => $this->_arrParam['status'],
		    );
		    $data = array(
	            'name' => (string) '%'.str_replace(' ','%',$name).'%',
	            'start_date' => $start_date,
		    );
		    $adapter = new Zend_Paginator_Adapter_DbSelect($this->_model->searchItem($data));
		    $paginator = new Zend_Paginator($adapter);
		    $paginator->setItemCountPerPage(40);
		    $paginator->setPageRange(5);
		    $currentPage = $this->_request->getParam('page', 1);
		    $paginator->setCurrentPageNumber($currentPage);
		    $this->view->data = $paginator;
		    $this->view->total = $paginator->getTotalItemCount();
		}
    }
